package cn.lili.controller.feign;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GrabOrderClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.grab.GrabNoticeParam;
import cn.lili.modules.grab.GrabOrderParam;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.reps.order.CreateOrderRespose;
import cn.lili.modules.grab.dingdong.reps.order.FreightCalcRespose;
import cn.lili.modules.grab.dingdong.res.notice.OrderCallbackRequest;
import cn.lili.modules.grab.dingdong.res.order.CreateOrderRequest;
import cn.lili.modules.grab.dingdong.res.order.FreightCalcRequest;
import cn.lili.modules.grab.dingdong.service.DingdongNoticeService;
import cn.lili.modules.grab.dingdong.service.DingdongOrderService;
import cn.lili.modules.grab.enums.SourceEnums;
import cn.lili.modules.grab.factory.notice.GrabNoticeFactory;
import cn.lili.modules.grab.factory.order.GrabOrderFactory;
import cn.lili.modules.grab.xdxt.reps.order.OrderFreightPreviewReps;
import cn.lili.modules.grab.xdxt.res.order.OrderFreightPreviewReq;
import cn.lili.modules.grab.xdxt.service.XdxtOrderServie;
import cn.lili.modules.order.order.client.OrderItemClient;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequiredArgsConstructor
@Slf4j
public class GrabOrderFeginController implements GrabOrderClient {

    @Resource
    private GoodsService goodsService;
    @Resource
    private GrabOrderFactory grabOrderFactory;
    @Resource
    private DingdongOrderService dingdongOrderService;
    @Resource
    private OrderItemClient orderItemClient;
    @Resource
    private DingdongNoticeService dingdongNoticeService;
    @Resource
    private XdxtOrderServie xdxtOrderServie;
    @Resource
    private GrabNoticeFactory grabNoticeFactory;

    @Override
    public void grabOrder(GrabOrderParam grabOrderParam) {
        boolean isHaiGuan = false;
        for (OrderItem orderItem : grabOrderParam.getOrderItems()) {
            //查询商品
            Goods goods = goodsService.getById(orderItem.getGoodsId());
            log.info("推送订单,根据goodsId查询,结果为{}", goods != null ? JSONObject.toJSON(goods) : null);
            if (goods.getThirdPartyPlatformType()!=null && 1 == goods.getThirdPartyPlatformType()) {
                //走壹邦玖业务
                log.info("推送订单,走壹邦玖业务");
            }
            if (goods.getThirdPartyPlatformType()!=null && 2 == goods.getThirdPartyPlatformType()) {
                //走企叮咚
                log.info("推送订单,走企叮咚");
                grabOrderParam.setOrderItem(orderItem);
                grabOrderParam.setCode(SourceEnums.DINGDONG_ORDER.name());
                grabOrderFactory.generateCreateOrderFactory(grabOrderParam);
            }
            if (goods.getThirdPartyPlatformType()!=null && 3 == goods.getThirdPartyPlatformType()) {
                //走盛创汇联
                log.info("推送订单,走盛创汇联,{}", JSONObject.toJSONString(goods));
                grabOrderParam.setOrderItem(orderItem);
                grabOrderParam.setCode(SourceEnums.XDXT_ORDER.name());
                grabOrderFactory.generateCreateOrderFactory(grabOrderParam);
            }
            log.info("推送订单,1121");
            if (goods.getThirdPartyPlatformType()!=null && 4 == goods.getThirdPartyPlatformType()) {
                log.info("推送订单,海关保税仓,{}", JSONObject.toJSONString(goods));
                isHaiGuan = true;
            }
            log.info("推送订单,1112221");
        }
        if (isHaiGuan) {
            //走海关保税仓
            grabOrderParam.setCode(SourceEnums.HAIGUAN_ORDER.name());
            grabOrderFactory.generateCreateOrderFactory(grabOrderParam);
        }
    }

    @Override
    public ResultMessage<Object> retryCreateOrder(CreateOrderRequest createOrderRequest) {
        CreateOrderRespose createOrderRespose = dingdongOrderService.createOrder(createOrderRequest);

        //更新子订单
        OrderItem orderItem = orderItemClient.getBySn(createOrderRequest.getTrade_no());
        orderItem.setOrderJsonVal(JSONObject.toJSONString(createOrderRespose));
        orderItem.setOrderItemIdVal(createOrderRespose.getOrder_sn());
        orderItemClient.updateById(orderItem);
        return ResultUtil.data(createOrderRespose);
    }

    @Override
    public ResultMessage<Object> retryCreateOrderCallback(OrderCallbackRequest orderCallbackRequest) {
        BaseResult baseResult = dingdongNoticeService.orderCallback(orderCallbackRequest);
        return ResultUtil.data(baseResult);
    }


    @Override
    public FreightCalcRespose freightCalc(FreightCalcRequest freightCalcRequest) {
        return dingdongOrderService.freightCalc(freightCalcRequest);
    }

    @Override
    public OrderFreightPreviewReps xdxtFreightCalc(OrderFreightPreviewReq orderFreightPreviewReq) {
        try {
            return xdxtOrderServie.freightCalc(orderFreightPreviewReq);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BaseResult goodsCallback(GrabNoticeParam grabParam) {
        return grabNoticeFactory.goodsCallback(grabParam);
    }

    @Override
    public BaseResult orderCallback(GrabNoticeParam grabParam) {
        return grabNoticeFactory.orderCallback(grabParam);
    }
}
